package com.wwh.memo.ui

import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.TextView
import androidx.recyclerview.widget.RecyclerView
import com.wwh.memo.R
import com.wwh.memo.logic.model.Memo

class MemoAdapter(private val memoList: List<Memo>): RecyclerView.Adapter<MemoAdapter.ViewHolder>(){  // RecyclerView的适配器
    inner class ViewHolder (view : View) : RecyclerView.ViewHolder(view) {
        val memoTitle: TextView = view.findViewById(R.id.memoTitle)
        val memoContent: TextView = view.findViewById(R.id.memoContent)
        val memoDatetime: TextView = view.findViewById(R.id.memoDatetime)
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {  // 设置点击备忘录标题项时反应
        val view = LayoutInflater.from(parent.context).inflate(R.layout.memo_item, parent, false)
        val holder = ViewHolder(view)
        holder.itemView.setOnClickListener {
            val memo = memoList[holder.bindingAdapterPosition]
            MemoContentActivity.actionStart(parent.context, memo)
        }
        return holder
    }

    override fun getItemCount(): Int {
        return memoList.size
    }

    override fun onBindViewHolder(holder: ViewHolder, position: Int) {  // 滚动时刷新界面
        val memo = memoList[position]
        holder.memoTitle.text = memo.title
        holder.memoContent.text = memo.content
        holder.memoDatetime.text = memo.datetime
    }


}
